/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import com.openbrew.openbrewrf.InvSalesLines;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import tools.InventoryModifiers;
import tools.SaleTools;

/**
 *
 * @author dchapman
 */
@Stateless
@Path("com.openbrew.openbrewrf.invsaleslines")
public class InvSalesLinesFacadeREST extends AbstractFacade<InvSalesLines> {
    @PersistenceContext(unitName = "com.openBrew_openBrewRF_war_1.0PU")
    private EntityManager em;
    SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

    public InvSalesLinesFacadeREST() {
        super(InvSalesLines.class);
    }

    @POST
    @Override
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public InvSalesLines create(@WebParam(name = "invsaleslines") InvSalesLines entity) {
        entity = super.create(entity);
        return entity;
    }

    @POST
    @Path("createTriggerWithAllocation")
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public InvSalesLines createTriggerWA(@WebParam(name = "sales") InvSalesLines entity) {
        entity = super.create(entity);
        final SaleTools st = new SaleTools();
        st.doInvSaleInsertionActions(em, entity, true);
        return entity;
    }

    @POST
    @Path("createTriggerWithoutAllocation")
    @WebMethod(operationName = "create")
    @Consumes({"application/xml", "application/json"})
    public InvSalesLines createTriggerWOA(@WebParam(name = "sales") InvSalesLines entity) {
        entity = super.create(entity);
        final SaleTools st = new SaleTools();
        st.doInvSaleInsertionActions(em, entity, false);
        return entity;
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(InvSalesLines entity) {
        final InventoryModifiers im = new InventoryModifiers();
        im.doInvSaleLineCheck(em, entity);
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        final InventoryModifiers im = new InventoryModifiers();
        InvSalesLines saleLine = super.find(id);
        super.remove(saleLine);
        im.doSupplyReturnWithoutAllocation(em, saleLine.getInvSaleSupplyID().getSupID()
                , saleLine.getInvSaleQty());
        em.getEntityManagerFactory().getCache().evictAll();
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public InvSalesLines find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<InvSalesLines> findAll() {
        return super.findAll();
    }
    
    @GET
    @Path("runQuery/{str}")
    @Override
    @Produces({"application/xml", "application/json"})
    public List<InvSalesLines> runQuery(@PathParam("str") String str){
        return super.runQuery(str);
    }
    
    @GET
    @Path("runBetweenDateQuery/{str}/{start}/{stop}")
    @Override
    @Produces({"application/xml", "application/json"})
    public List<InvSalesLines> runBetweenDateQuery(@PathParam("str") String str
            , @PathParam("start") Date start
            , @PathParam("stop") Date stop){
        return super.runBetweenDateQuery(str, start, stop);
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<InvSalesLines> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
}
